import formDesigner from '@common/form-designer';

import dataTableItem from './views/form-change-rule/data-table-item.vue';
import basic from '@common/basic'
import { request } from '@common/core';
import { merge } from 'lodash-es'; 
import substitutionPartTable from './business-module/part/views/alternative-rule/substitution-rules-model/components/substitution-part-table.vue'
const originW = tyRoot.getExportResource('src/modules/pdm/business-module/part/views/alternative-rule/substitution-rules-model/components/substitution-part-table.vue')
merge(originW,{
  default:substitutionPartTable
})
const replaceComponent = {
  'src/form-designer/extension/form-render/data-table-item.vue': dataTableItem,
  
};

Object.keys(replaceComponent).forEach((key) => {
  merge(formDesigner.getExportResource(key), {
    default: replaceComponent[key]
  });
});


console.log('%c二开逻辑执行完成111111', 'color: red;font-size:20px;');

const useOperate = basic.getExportResource('src/basic/components/operate-iteration/js/operate.js').default;
console.log(window.tyRoot);

import { ElMessage } from 'element-plus'

const creatUpdateRule = window.tyRoot.getExportResource('src/modules/pdm/business-module/part/views/alternative-rule/composables/table-row-operate.js')
merge(creatUpdateRule, {
  useOverride(props, emits) {
    const origin = creatUpdateRule.useTableRowOperate(props, emits)
    console.log('origin111', origin);
    const { wujieName } = window.$wujie?.props || {};
    const { operateCommand } = useOperate({ moduleName: `${wujieName}` }, {});

    const btnLoadingMap = origin.btnLoadingMap
    return {
      ...origin,
      btnOptions: computed(() => {
        return (row, type) => {
          let obj = {
            hyperlink: {
              type: 'text',
              iconClass: 'iconfont icon-operation-link1',
            },
            checkin: {
              type: 'text',
              iconClass: 'iconfont icon-a-structure-checkin',
            },
            checkout: {
              type: 'text',
              iconClass: 'iconfont icon-a-structure-checkout',
              loading: btnLoadingMap.value['CheckOut']
            },
            revoke: {
              type: 'text',
              iconClass: 'iconfont icon-structure-revoke',
              loading: btnLoadingMap.value['UndoCheckOut']
            },
            edit: {
              type: 'text',
              iconClass: 'iconfont icon-operation-edit1',
              loading: btnLoadingMap.value['edit']
            },
            revise: {
              type: 'text',
              iconClass: 'iconfont icon-operation-edit1',
            }
          };
          return obj[type];
        };
      }),
      dropdownOptions: computed(() => {
        return row => {
          let btnDisabled = row.lockstateinfo == 'c/i';
          return {
            trigger: 'click',
            list: [
              {
                icon: 'iconfont icon-shezhi',
                label: '设置状态',
                value: 'status',
                disabled: !btnDisabled || !window.$wujie?.props?.calcResult2('设置状态', [row])
              },
              {
                icon: 'iconfont icon-a-zhongmingming1',
                label: '重命名',
                value: 'rename',
                disabled: !btnDisabled || !window.$wujie?.props?.calcResult2('重命名', [row])
              },
              {
                icon: 'iconfont icon-yonghu',
                label: '设置所有者',
                value: 'owner',
                disabled: !btnDisabled || !window.$wujie?.props?.calcResult2('设置所有者', [row])
              },
              {
                icon: 'iconfont icon-a-liuchengzhong2',
                label: '新建流程',
                value: 'process',
                disabled: !btnDisabled
              },
              {
                icon: 'iconfont icon-bianjifangwenkongzhiquanxian',
                label: '编辑访问控制权限',
                value: 'editaccess'
              },

              {
                icon: 'iconfont icon-a-liuchengzhong2',
                label: '修订',
                value: 'revise',
                disabled: !btnDisabled || !window.$wujie?.props?.calcResult2('修订', [row])
              },
              {
                icon: 'iconfont icon-operation-delete',
                label: '删除',
                value: 'delete',
                disabled: !btnDisabled || !window.$wujie?.props?.calcResult2('删除', [row])
              }
            ]
          };
        };
      }),
      handleCommand: async (row, command) => {
        switch (command) {
          case 'status':
            operateCommand['setStatus'] && operateCommand['setStatus']([row]);
            emits('getCurrentRow', props.tableTypeRef, [row]);
            break;
          case 'rename':
            operateCommand['rename'] && operateCommand['rename']([row]);
            emits('getCurrentRow', props.tableTypeRef, [row]);
            break;
          case 'owner':
            operateCommand['setOwner'] && operateCommand['setOwner']([row]);
            emits('getCurrentRow', props.tableTypeRef, [row]);
            break;
          case 'process':
            window.$wujie?.bus.$emit('partReplaceRuleAddProcess', [row], wujieName);
            break;
          case 'editaccess':
            mainObj.value = row;
            editaccessShow.value = true;
            break;
          case 'revise':
            if (row.lifecyclestagekey != 'RELEASED') {
              alert('非已发布的替代规则不能修订')
              break;
            }
            console.log('oid', row.oid)
            const res = await request({
              method: 'post',
              url: '/rest/v1/api/bom/rule',
              data: {
                oid: row.oid,
              },
              headers: {
                authorization: sessionStorage.getItem('authorization'),
                'Content-Type': 'application/x-www-form-urlencoded'
              }
            });
            console.log('jiegou', res)
            if (res.data.success) {
              // console.log("刷新逻辑")
              // let data = {
              //   "columns": [
              //     {
              //       "propertyName": "typeIcon",
              //       "displayName": "双图标",
              //       "masterProperty": false,
              //       "fixed": "",
              //       "width": "60",
              //       "sortable": false,
              //       "fieldList": [],
              //       "virtual": false,
              //       "foreignColumnMaster": false,
              //       "canNotRemove": false,
              //       "enum": false
              //     },
              //     {
              //       "propertyName": "objectNumber",
              //       "displayName": "编号",
              //       "masterProperty": false,
              //       "fixed": "",
              //       "width": "140",
              //       "sortable": false,
              //       "fieldList": [],
              //       "virtual": false,
              //       "foreignColumnMaster": false,
              //       "canNotRemove": false,
              //       "enum": false
              //     },
              //     {
              //       "propertyName": "name",
              //       "displayName": "名称",
              //       "masterProperty": false,
              //       "fixed": "",
              //       "width": "160",
              //       "sortable": false,
              //       "fieldList": [],
              //       "virtual": false,
              //       "foreignColumnMaster": false,
              //       "canNotRemove": false,
              //       "enum": false
              //     },
              //     {
              //       "propertyName": "operate",
              //       "displayName": "操作",
              //       "masterProperty": false,
              //       "fixed": "",
              //       "width": "100",
              //       "sortable": false,
              //       "fieldList": [],
              //       "virtual": false,
              //       "foreignColumnMaster": false,
              //       "canNotRemove": false,
              //       "enum": false
              //     },
              //     {
              //       "propertyName": "multiAlternateForPart",
              //       "displayName": "被替代件",
              //       "masterProperty": false,
              //       "fixed": "",
              //       "width": "180",
              //       "sortable": false,
              //       "fieldList": [],
              //       "virtual": false,
              //       "foreignColumnMaster": false,
              //       "canNotRemove": false,
              //       "enum": false
              //     },
              //     {
              //       "propertyName": "multiAlternatePart",
              //       "displayName": "替代件",
              //       "masterProperty": false,
              //       "fixed": "",
              //       "width": "180",
              //       "sortable": false,
              //       "fieldList": [],
              //       "virtual": false,
              //       "foreignColumnMaster": false,
              //       "canNotRemove": false,
              //       "enum": false
              //     },
              //     {
              //       "propertyName": "alternatescope",
              //       "displayName": "替代范围",
              //       "masterProperty": false,
              //       "fixed": "",
              //       "width": "200",
              //       "sortable": false,
              //       "fieldList": [],
              //       "virtual": false,
              //       "foreignColumnMaster": false,
              //       "canNotRemove": false,
              //       "enum": false
              //     },
              //     {
              //       "propertyName": "description",
              //       "displayName": "摘要",
              //       "masterProperty": false,
              //       "fixed": "",
              //       "width": "240",
              //       "sortable": false,
              //       "fieldList": [],
              //       "virtual": false,
              //       "foreignColumnMaster": false,
              //       "canNotRemove": false,
              //       "enum": false
              //     },
              //     {
              //       "propertyName": "lifecyclestagekeyName",
              //       "displayName": "状态",
              //       "masterProperty": false,
              //       "fixed": "",
              //       "width": "100",
              //       "sortable": false,
              //       "fieldList": [],
              //       "virtual": false,
              //       "foreignColumnMaster": false,
              //       "canNotRemove": false,
              //       "enum": false
              //     },
              //     {
              //       "propertyName": "creatorDisplayName",
              //       "displayName": "创建者",
              //       "masterProperty": false,
              //       "fixed": "",
              //       "width": "100",
              //       "sortable": false,
              //       "fieldList": [],
              //       "virtual": false,
              //       "foreignColumnMaster": false,
              //       "canNotRemove": false,
              //       "enum": false
              //     }
              //   ],
              //   "pageNum": 1,
              //   "pageSize": 20,
              //   "needCount": true,
              //   "multiAlternateForPartList": [],
              //   "multiAlternatePartList": [],
              //   "groupBy": "multiAlternateForPart"
              // }
              // let res =await request({
              //   method: 'post',
              //   url: '/rest/v1/partMultiAlternate/queryPartMultiAlternateGroupForPage',
              //   data,
              //   headers: {
              //     authorization: sessionStorage.getItem('authorization'),
              //   }
              // })
              // console.log("我是新增的刷新逻辑",res)
             ElMessage.success('修订成功！');
              const mainAppWindow = window.$wujie?.parentWindow || window.top;
              setTimeout(()=>{
                mainAppWindow?.location.reload(true);
              },500)
            }
            break;
          case 'delete':
            origin.handleDeleteCurrentList({
              list: [row],
              wujieName: wujieName
            });
            break;
          default:
            break;
        }
      }
    }
  }
})